﻿using IOP.Extension.DependencyInjection;
using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace VkSample101
{
    [ModulePriority(ModulePriority.Assets)]
    public class AssetsModule : VulkanModule, IVulkanRenderWapperModule
    {
        public VulkanRenderWapper RenderWapper { get; set; }
        [Autowired]
        private ILogger<AssetsModule> Logger { get; set; }

        protected override async Task Load(VulkanGraphicsManager graphicsManager)
        {
            try
            {
                var basePath = AppContext.BaseDirectory;
                var texPath = Path.Combine(basePath, "Assets", "Textures");
                var modelPath = Path.Combine(basePath, "Assets", "Models");
                var lDevice = graphicsManager.VulkanDevice;

                ISEADocument document = new();
                await document.Load(Path.Combine(AppContext.BaseDirectory, "test101.isea"));
                var renders = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document);
                graphicsManager.TryGetRenderGroup("OBJ", out IRenderGroup renderGroup);
                
                var arch = ContextManager.CreateArchetype(new TransformComponent());
                var lightArchetype = ContextManager.CreateArchetype(new LightComponent());
                foreach (var item in renders)
                {
                    switch (item.Name)
                    {
                        case "Ball":
                            ContextManager.CreateContexts(1, arch, renderGroup, item, new Transform() { Position = new Vector3(-10, 0, 0), Rotation = Quaternion.Identity, Scaling = Vector3.One });
                            break;
                        case "Box":
                            ContextManager.CreateContexts(1, arch, renderGroup, item, new Transform() { Position = new Vector3(10, 0, 0), Rotation = Quaternion.Identity, Scaling = Vector3.One });
                            break;
                        case "Plane":
                            ContextManager.CreateContexts(1, arch, renderGroup, item, new Transform() { Position = new Vector3(0, 0, 0), Rotation = Quaternion.Identity, Scaling = new Vector3(0.1f, 0.1f, 0.1f) });
                            break;
                        case "Ghxp":
                            ContextManager.CreateContexts(1, arch, renderGroup, item, new Transform() { Position = new Vector3(0, 0, 8), Rotation = Quaternion.Identity, Scaling = Vector3.One });
                            break;
                        case "Yh":
                            ContextManager.CreateContexts(1, arch, renderGroup, item, new Transform() { Position = new Vector3(0, 0, -8), Rotation = Quaternion.Identity, Scaling = Vector3.One });
                            break;
                        default:
                            continue;
                    }
                }
                var light = graphicsManager.CreatePointLight<RenderObject>("main", new Vector3(100, 100, 100), new LightStrength(0.7f, 0.7f, 0.7f, 0.7f));
                ContextManager.CreateContexts(1, lightArchetype, renderGroup, light);
            }
            catch (Exception e)
            {
                Logger.LogError(e.Message + "\r\n" + e.StackTrace);
            }
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
